"""
该文件仅用于验证数据集是否符合规范，核心代码与main.py一致。测试时不需要使用
Used to verify that the dataset is valid
"""
import os
import cv2
import math
import hand_extraction as he
import config


# 路径全局变量
FILE_ROUTE = 'D:/Products/cache/'
# 模型超参数
REVISE_WIDTH = config.REVISE_WIDTH
REVISE_HEIGHT = config.REVISE_HEIGHT
MIN_ANGLE = config.MIN_ANGLE
MIN_DISTANCE = config.MIN_DISTANCE
MAX_DISTANCE = config.MAX_DISTANCE


if __name__ == '__main__':

    inner = 1
    while True:
        video_name = FILE_ROUTE + "t (" + str(inner) + ").mp4"
        is_video_exists = os.path.exists(video_name)
        if not is_video_exists:
            print('No video called: ', video_name)
            break
        cen_point = []   # 轨迹质心
        key_point = []    # 轨迹关键点
        angle = []  # 角度
        angle_out = []  # 输出的角度
        distance_out = []   # 输出的距离
        print('<------Sample', str(inner), '------>')
        cap = cv2.VideoCapture(video_name)  # 获取视频流
        while True:
            ret, frame = cap.read()
            if frame is None:   # 判断视频流中的帧是否存在
                break
            if frame.shape[1] > frame.shape[0]:
                frame = cv2.transpose(frame)
                frame = cv2.flip(frame, 1)
            frame = cv2.resize(frame, (REVISE_WIDTH, REVISE_HEIGHT))

            # 创建全黑图
            show_img = frame.copy()
            show_img[:, :] = 0

            # 获取手势质心
            center = he.get_centroid(frame)
            cv2.circle(show_img, center, 4, (255, 0, 0), cv2.FILLED)

            # 获取关键点之间的角度与距离
            if len(cen_point) == 0:
                key_point.append(center)
            elif len(cen_point) == 1:
                angle.append(he.get_angle(cen_point[-1], center))
            else:
                condition_ang = math.fabs(he.get_angle(cen_point[-1], center) - angle[-1]) > MIN_ANGLE
                condition_dis = he.get_distance(center, key_point[-1]) > MAX_DISTANCE
                if condition_ang or condition_dis:
                    if he.get_distance(center, key_point[-1]) > MIN_DISTANCE:
                        angle.append(he.get_angle(cen_point[-1], center))
                        key_point.append(center)
                        cur_angle = round(he.get_angle(key_point[-2], key_point[-1]) / (2 * math.pi), 4)
                        cur_distance = round(he.get_distance(key_point[-2], key_point[-1]), 2)
                        angle_out.append(cur_angle)
                        distance_out.append(cur_distance)
                        print('(', cur_angle, ', ', cur_distance, ')')
            cen_point.append(center)

            # 绘制轨迹和关键点
            for i in range(len(key_point)):
                cv2.circle(show_img, key_point[i], 2, (0, 0, 255), cv2.FILLED)
                if i > 0:
                    cv2.line(show_img, key_point[i], key_point[i - 1], (255, 255, 255), 1)
            cv2.namedWindow("show", 0)
            cv2.resizeWindow("show", 540, 960)
            cv2.imshow("show", show_img)
            if cv2.waitKey(1) & 0xff == ord('q'):
                raise KeyboardInterrupt('User interruption!')
        cap.release()
        while True:
            print('Please judge this video: ')
            judge = input()
            if judge == 'n':
                os.remove(FILE_ROUTE + "t (" + str(inner) + ").mp4")
                print('Already delete this video!')
                break
            elif judge == 'y':
                break
        inner += 1
    cv2.destroyAllWindows()
